import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Product",urlPatterns = "/product.do")
public class ProductsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String successJson="{\"msg\":\"success\",\"data\":{\"info\":\"[{\"pid\":10002,\"pname\":\"弹头传奇4元\",\"fee\":400,\"otype\":\"点播\"}," +
                "{\"pid\":10001,\"pname\":\"弹头传奇1元\",\"fee\":100,\"otype\":\"点播\"},{\"pid\":10000,\"pname\":\"测试计费点1分\"," +
                "\"fee\":1,\"otype\":\"点播\"}]\"},\"status\":true}";
        String faildJson="{\"msg\":\"渠道编码有误\",\"status\":\"false\"}";
        String cid=request.getParameter("cid");
        if (!"".equals(cid)&&cid!=null){
            if (Integer.parseInt(cid)==1203){
                responseContent(response,successJson);
            }else{
                responseContent(response,faildJson);
            }
        }else{
            responseContent(response,faildJson);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        doPost(request,response);
    }

    private void responseContent(HttpServletResponse response,String content) throws IOException {
        PrintWriter writer=response.getWriter();
        writer.write(content);
        writer.close();
    }
}
